package cloud.seri.auth.web.rest

import cloud.seri.auth.service.RoleService
import cloud.seri.auth.web.rest.errors.BadRequestAlertException
import cloud.seri.auth.service.dto.RoleDTO

import io.github.jhipster.web.util.HeaderUtil
import io.github.jhipster.web.util.ResponseUtil
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

import javax.validation.Valid
import java.net.URI
import java.net.URISyntaxException

/**
 * REST controller for managing [cloud.seri.auth.domain.Role].
 */
@RestController
@RequestMapping("/api")
class RoleResource(
    val roleService: RoleService
)
{

    private val log = LoggerFactory.getLogger(this.javaClass)

    @Value("\${jhipster.clientApp.name}")
    private var applicationName: String? = null

    /**
     * `POST  /roles` : Create a new role.
     *
     * @param roleDTO the roleDTO to create.
     * @return the [ResponseEntity] with status `201 (Created)` and with body the new roleDTO, or with status `400 (Bad Request)` if the role has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/roles")
    fun createRole(@Valid @RequestBody roleDTO: RoleDTO): ResponseEntity<RoleDTO>
    {
        log.debug("REST request to save Role : {}", roleDTO)
        if (roleDTO.id != null)
        {
            throw BadRequestAlertException("A new role cannot already have an ID", ENTITY_NAME, "idexists")
        }
        val result = roleService.save(roleDTO)
        return ResponseEntity.created(URI("/api/roles/" + result.id))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.id.toString()))
            .body(result)
    }

    /**
     * `PUT  /roles` : Updates an existing role.
     *
     * @param roleDTO the roleDTO to update.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the updated roleDTO,
     * or with status `400 (Bad Request)` if the roleDTO is not valid,
     * or with status `500 (Internal Server Error)` if the roleDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/roles")
    fun updateRole(@Valid @RequestBody roleDTO: RoleDTO): ResponseEntity<RoleDTO>
    {
        log.debug("REST request to update Role : {}", roleDTO)
        if (roleDTO.id == null)
        {
            throw BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull")
        }
        val result = roleService.save(roleDTO)
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, roleDTO.id.toString()))
            .body(result)
    }

    /**
     * `GET  /roles` : get all the roles.
     *
     * @return the [ResponseEntity] with status `200 (OK)` and the list of roles in body.
     */
    @GetMapping("/roles")
    fun getAllRoles(): MutableList<RoleDTO>
    {
        log.debug("REST request to get all Roles")
        return roleService.findAll()
    }

    /**
     * `GET  /roles/:id` : get the "id" role.
     *
     * @param id the id of the roleDTO to retrieve.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the roleDTO, or with status `404 (Not Found)`.
     */
    @GetMapping("/roles/{id}")
    fun getRole(@PathVariable id: String): ResponseEntity<RoleDTO>
    {
        log.debug("REST request to get Role : {}", id)
        val roleDTO = roleService.findOne(id)
        return ResponseUtil.wrapOrNotFound(roleDTO)
    }

    /**
     * `DELETE  /roles/:id` : delete the "id" role.
     *
     * @param id the id of the roleDTO to delete.
     * @return the [ResponseEntity] with status `204 (NO_CONTENT)`.
     */
    @DeleteMapping("/roles/{id}")
    fun deleteRole(@PathVariable id: String): ResponseEntity<Void>
    {
        log.debug("REST request to delete Role : {}", id)
        roleService.delete(id)
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id)).build()
    }

    companion object
    {
        private const val ENTITY_NAME = "role"
    }
}
